package cn.edu.ctbu.dp2.api;

import cn.edu.ctbu.dp2.constant.REnum;
import cn.edu.ctbu.dp2.entity.Teacher;
import cn.edu.ctbu.dp2.exception.RException;
import cn.edu.ctbu.dp2.service.TeacherService;
import cn.edu.ctbu.dp2.util.RUtil;
import cn.edu.ctbu.dp2.vo.QueryObj;
import cn.edu.ctbu.dp2.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {

    @Autowired
    private TeacherService teacherService;



    @GetMapping("/list")
    public R<List<Teacher>> findAll(){
        List<Teacher>teachers=teacherService.getAll();
        return RUtil.success(teachers);
    }


    @GetMapping("/{id}")
    public R<Teacher>  findById(@PathVariable int id){
        Teacher teacher=teacherService.findById(id);
        return RUtil.success(teacher);
    }
    /**
     * 增加老师
     * @param teacher
     * @return
     */
    @PostMapping("/add")
    public R<Teacher> add(Teacher teacher) {
        return RUtil.success(teacherService.add(teacher));
    }
    /**
     * 更新老师信息
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public R<Teacher> update(Teacher teacher) {
        return RUtil.success(teacherService.update(teacher));
    }
    /**
     * 删除学生信息
     * @param id
     */
    @DeleteMapping("/delete")
    public R<?> deleteMultiple(@RequestBody List<Integer> ids) {
        teacherService.deleteMultiple(ids);
        return RUtil.success();
    }


    @GetMapping("/validateUser")
    public R validateUser(String tno, String password)throws Exception {
        return RUtil.success(teacherService.validateUsernameAndPassword(tno,password));
    }

    @PostMapping("/getbypage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> qObj) {
        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.DESC,"id");
        Integer pageIndex=0;
        Integer pageSize=10;

        if(qObj==null){
            //名字为空就直接调用分页
            Pageable pageable= PageRequest.of(pageIndex,pageSize,sort);
            Page<Teacher> teachers=teacherService.getAll(pageable);
            return RUtil.success(teachers.getContent(),teachers.getTotalElements());
        }else{
            pageIndex=qObj.getPage()-1;
            pageSize=qObj.getLimit();
            if(qObj.getData() instanceof Teacher){
                Teacher teacher=(Teacher) qObj.getData();
                Pageable pageable=PageRequest.of(pageIndex,pageSize,sort);
                ExampleMatcher matcher=ExampleMatcher.matching()
                        .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains()).withIgnoreNullValues();
                Example<Teacher>example=Example.of(teacher,matcher);
                Page<Teacher>teacherPage=teacherService.getAll(example,pageable);
                return RUtil.success(teacherPage.getContent(),teacherPage.getTotalElements());
            }else{
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }
}
